<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="smallmuou&#39;s blog">
<meta property="og:url" content="http://smallmuou.gitee.io/blog/page/2/index.html">
<meta property="og:site_name" content="smallmuou&#39;s blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="smallmuou&#39;s blog">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://smallmuou.gitee.io/blog/page/2/"/>





  <title>smallmuou's blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">smallmuou's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"><a href="https://github.com/smallmuou">https://github.com/smallmuou</a></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/09/03/2018-09-03-百度云加速器 - baiduyun-accelerator/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/09/03/2018-09-03-百度云加速器 - baiduyun-accelerator/" itemprop="url">百度云加速器 - baiduyun-accelerator</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-09-03T00:00:00+08:00">
                2018-09-03
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/工具/" itemprop="url" rel="index">
                    <span itemprop="name">工具</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>随着百度云限速的到来，速度已经降到100KB/s以内，每次下载都很烦恼；该工具是为了解决此痛点而生。该工具使用了 HTTP RANGE原理进行分段下载，可以将一个文件分成N块来下载，这样下载速度就有可能提升N倍（理论上）</p>
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><p>下载baiduyun-accelerator至本地: <a href="https://github.com/smallmuou/baiduyun-accelerator" target="_blank" rel="noopener">https://github.com/smallmuou/baiduyun-accelerator</a></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">/bin/bash baiduyun-accelerator redirection-url [split-number]</span><br><span class="line"></span><br><span class="line">redirection-url是重定向的下载地址，可以在浏览器中点击下载，在下载队列 - 详情中找到Redirection；注意该地址会在一段时间后过期</span><br><span class="line">split-number是分块的数目，默认10个；理论上配置越大越好，但要考虑时间CPU、文件大小、带宽；因此配置越大，下载速度并不一定越大，具体可以在实际网络中测试。</span><br></pre></td></tr></table></figure>
<h3 id="对比结果"><a href="#对比结果" class="headerlink" title="对比结果"></a>对比结果</h3><p>单个（浏览器）下载速度<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">smou:baiduyun-accelerator $ /bin/bash baiduyun-accelerator <span class="string">'https://d11.baidupcs.com/file/1775ee70ef849f6b442c7540868a3f25?bkt=p3-00008c600f8c69bb2150622466d47de9ab8a&amp;xcode=671f0e660fc919180776ea6ad9d4e9bcd63f0c04783d1f719cd11a99d56a137169272f8fe280ef0947ee41204567a91c&amp;fid=87338191-250528-212614922664897&amp;time=1535955568&amp;sign=FDTAXGERLQBHSK-DCb740ccc5511e5e8fedcff06b081203-X8HP4ShkOgbIadwToFqXzXHoTwU%3D&amp;to=d11&amp;size=24908868&amp;sta_dx=24908868&amp;sta_cs=177437&amp;sta_ft=ape&amp;sta_ct=6&amp;sta_mt=6&amp;fm2=MH%2CYangquan%2CAnywhere%2C%2Cfujian%2Cct&amp;resv0=cdnback&amp;resv1=0&amp;vuk=282335&amp;iv=0&amp;htype=&amp;newver=1&amp;newfm=1&amp;secfm=1&amp;flow_ver=3&amp;pkey=00008c600f8c69bb2150622466d47de9ab8a&amp;sl=76480590&amp;expires=8h&amp;rt=sh&amp;r=233439886&amp;mlogid=9154848837080176738&amp;vbdid=3049029286&amp;fin=%E8%AF%B4%E6%95%A3%E5%B0%B1%E6%95%A3-%E5%89%8D%E4%BB%BB3.ape&amp;fn=%E8%AF%B4%E6%95%A3%E5%B0%B1%E6%95%A3-%E5%89%8D%E4%BB%BB3.ape&amp;rtype=1&amp;dp-logid=9154848837080176738&amp;dp-callid=0.1.1&amp;hps=1&amp;tsl=80&amp;csl=80&amp;csign=ZMLyV6T0L9zkkwFfMOo%2F4sxc4LA%3D&amp;so=0&amp;ut=6&amp;uter=4&amp;serv=0&amp;uc=359584213&amp;ti=26fa64dbec28822482b9f079f8a5dcf2fbcbbbe60893ad2e305a5e1275657320&amp;by=themis'</span> 1</span><br><span class="line">Progress: 0%     0KB/s</span><br><span class="line">Progress: 0%     84KB/s</span><br><span class="line">Progress: 0%     112KB/s</span><br><span class="line">Progress: 1%     64KB/s</span><br><span class="line">Progress: 1%     64KB/s</span><br><span class="line">Progress: 1%     128KB/s</span><br><span class="line">Progress: 2%     64KB/s</span><br><span class="line">Progress: 2%     100KB/s</span><br><span class="line">Progress: 2%     64KB/s</span><br><span class="line">Progress: 3%     64KB/s</span><br><span class="line">Progress: 3%     64KB/s</span><br><span class="line">Progress: 3%     128KB/s</span><br><span class="line">Progress: 4%     64KB/s</span><br><span class="line">Progress: 4%     64KB/s</span><br><span class="line">Progress: 4%     100KB/s</span><br><span class="line">Progress: 5%     64KB/s</span><br><span class="line">Progress: 5%     128KB/s</span><br></pre></td></tr></table></figure></p>
<p>提速下载（10个分块）结果</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">smou:baiduyun-accelerator $ /bin/bash baiduyun-accelerator <span class="string">'https://d11.baidupcs.com/file/1775ee70ef849f6b442c7540868a3f25?bkt=p3-00008c600f8c69bb2150622466d47de9ab8a&amp;xcode=671f0e660fc919180776ea6ad9d4e9bcd63f0c04783d1f719cd11a99d56a137169272f8fe280ef0947ee41204567a91c&amp;fid=87338191-250528-212614922664897&amp;time=1535955568&amp;sign=FDTAXGERLQBHSK-DCb740ccc5511e5e8fedcff06b081203-X8HP4ShkOgbIadwToFqXzXHoTwU%3D&amp;to=d11&amp;size=24908868&amp;sta_dx=24908868&amp;sta_cs=177437&amp;sta_ft=ape&amp;sta_ct=6&amp;sta_mt=6&amp;fm2=MH%2CYangquan%2CAnywhere%2C%2Cfujian%2Cct&amp;resv0=cdnback&amp;resv1=0&amp;vuk=282335&amp;iv=0&amp;htype=&amp;newver=1&amp;newfm=1&amp;secfm=1&amp;flow_ver=3&amp;pkey=00008c600f8c69bb2150622466d47de9ab8a&amp;sl=76480590&amp;expires=8h&amp;rt=sh&amp;r=233439886&amp;mlogid=9154848837080176738&amp;vbdid=3049029286&amp;fin=%E8%AF%B4%E6%95%A3%E5%B0%B1%E6%95%A3-%E5%89%8D%E4%BB%BB3.ape&amp;fn=%E8%AF%B4%E6%95%A3%E5%B0%B1%E6%95%A3-%E5%89%8D%E4%BB%BB3.ape&amp;rtype=1&amp;dp-logid=9154848837080176738&amp;dp-callid=0.1.1&amp;hps=1&amp;tsl=80&amp;csl=80&amp;csign=ZMLyV6T0L9zkkwFfMOo%2F4sxc4LA%3D&amp;so=0&amp;ut=6&amp;uter=4&amp;serv=0&amp;uc=359584213&amp;ti=26fa64dbec28822482b9f079f8a5dcf2fbcbbbe60893ad2e305a5e1275657320&amp;by=themis'</span> 10</span><br><span class="line">Progress: 0%     0KB/s</span><br><span class="line">Progress: 2%     680KB/s</span><br><span class="line">Progress: 5%     648KB/s</span><br><span class="line">Progress: 8%     716KB/s</span><br><span class="line">Progress: 11%    832KB/s</span><br><span class="line">Progress: 14%    760KB/s</span><br><span class="line">Progress: 17%    700KB/s</span><br><span class="line">Progress: 21%    928KB/s</span><br><span class="line">Progress: 23%    556KB/s</span><br><span class="line">Progress: 28%    1000KB/s</span><br><span class="line">Progress: 31%    760KB/s</span><br><span class="line">Progress: 34%    708KB/s</span><br><span class="line">Progress: 37%    844KB/s</span><br><span class="line">Progress: 40%    744KB/s</span><br><span class="line">Progress: 43%    808KB/s</span><br><span class="line">Progress: 47%    772KB/s</span><br><span class="line">Progress: 51%    1096KB/s</span><br><span class="line">Progress: 53%    580KB/s</span><br></pre></td></tr></table></figure>
<p>PS: 可以看出速度提升了8-9倍</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/08/27/2018-08-27-Linux的NICE时间/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/08/27/2018-08-27-Linux的NICE时间/" itemprop="url">Linux的NICE时间</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-27T00:00:00+08:00">
                2018-08-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>使用top命令时，会显示一个ni值，一直没留意，今特地研究了下：NICE表示进程的友好值，从-20到19，值越高，进程优先级越低（越友好，越不会抢占CPU）；NICE TIME是指NICE大于0的进程所占用CPU时间百分比</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">top - 14:53:27 up 58 min,  2 users,  load average: 0.22, 1.24, 1.60</span><br><span class="line">Tasks: 130 total,   2 running, 124 sleeping,   4 stopped,   0 zombie</span><br><span class="line">%Cpu(s):  0.0 us, 16.1 sy, 35.5 ni, 48.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st</span><br><span class="line">KiB Mem :  3877076 total,   182764 free,  3289944 used,   404368 buff/cache</span><br><span class="line">KiB Swap:  2097148 total,  2096884 free,      264 used.   319240 avail Mem</span><br><span class="line"></span><br><span class="line">  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND</span><br><span class="line">10877 root      22   2  113176   1180    996 R  93.8  0.0   0:02.91 loop.sh</span><br><span class="line"> 6093 root      20   0  160436   7436   4368 S   6.2  0.2   2:53.93 sshd</span><br><span class="line">10884 root      20   0  161972   2168   1520 R   6.2  0.1   0:00.16 top</span><br></pre></td></tr></table></figure>
<p>PS: 从以上可以看出NICE TIME=35.5%, loop.sh进程NICE为2，而实际优先级PR为22 （PR应该是从0开始，20对应NICE的0，这样NICE=-20，PR刚好为0）</p>
<p>进程优先级是继承父进程，一般为0，若要修改优先级，可以通过nice命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 优先级修改为2</span></span><br><span class="line">nice -n 2 ./loop.sh</span><br><span class="line"></span><br><span class="line"><span class="comment"># 未指定则修改为10</span></span><br><span class="line">nice ./loop.sh</span><br><span class="line"></span><br><span class="line"><span class="comment"># 优先级修改为-2</span></span><br><span class="line">nice -n -2 ./loop.sh</span><br></pre></td></tr></table></figure>
<p>若要更改一个正在执行的进程</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">renice -n 0 10877</span><br></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/08/24/2018-08-24-git忽略已追踪文件的变动/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/08/24/2018-08-24-git忽略已追踪文件的变动/" itemprop="url">git忽略已追踪文件的变动</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-24T00:00:00+08:00">
                2018-08-24
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/版本管理/" itemprop="url" rel="index">
                    <span itemprop="name">版本管理</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>经常遇到这种情况：需要某个文件，但又不想把本地修改同步至远端，因此不同电脑路径不一样，可以通过如下语句忽悠<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git update-index --assume-unchanged 文件路径</span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/08/20/2018-08-20-微信扫一扫自定义二维码实现支付/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/08/20/2018-08-20-微信扫一扫自定义二维码实现支付/" itemprop="url">微信扫一扫自定义二维码实现支付</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-20T00:00:00+08:00">
                2018-08-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/前端/" itemprop="url" rel="index">
                    <span itemprop="name">前端</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>众所周知，微信提供了APP、公众号、小程序、H5及扫码等支付方式，而本文需要实现微信”扫一扫”自定义二维码支付功能；<br>而扫码支付中用到的二维码是微信规范的二维码（weixin://），H5需要在非微信浏览器才能打开；因此只剩下了公众号（或小程序），本文基于公众号支付为大家讲解下整个流程</p>
<h3 id="步骤1-准备appid、appsecret、支付key"><a href="#步骤1-准备appid、appsecret、支付key" class="headerlink" title="步骤1. 准备appid、appsecret、支付key"></a>步骤1. 准备appid、appsecret、支付key</h3><p>进入微信公众平台，可以查到appid、appsecret；进入微信支付平台，可以找到支付key</p>
<h3 id="步骤2-获取openid"><a href="#步骤2-获取openid" class="headerlink" title="步骤2. 获取openid"></a>步骤2. 获取openid</h3><p>微信下单需要用到openid，因此需要先获取openid，可参考【<a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_4" target="_blank" rel="noopener">获取openid</a> 】：</p>
<h5 id="（1）按微信要求实现服务回调接口"><a href="#（1）按微信要求实现服务回调接口" class="headerlink" title="（1）按微信要求实现服务回调接口"></a>（1）按微信要求实现服务回调接口</h5><h5 id="（2）用户访问如下地址，获取code"><a href="#（2）用户访问如下地址，获取code" class="headerlink" title="（2）用户访问如下地址，获取code"></a>（2）用户访问如下地址，获取code</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&amp;redirect_uri=REDIRECT_URI&amp;response_type=code&amp;scope=SCOPE&amp;state=STATE#wechat_redirect</span></span><br></pre></td></tr></table></figure>
<p>PS: scope为snsapi_base可以实现用户免授权，如果是snsapi_userinfo需要用户同意，本需求采用snsapi_base</p>
<p>REDIRECT_URI地址需要在微信公众号配置，可以配置根路径</p>
<h5 id="（3）微信校验通过后，调用我们服务的redirect-uri-code-CODE-amp-state-STATE"><a href="#（3）微信校验通过后，调用我们服务的redirect-uri-code-CODE-amp-state-STATE" class="headerlink" title="（3）微信校验通过后，调用我们服务的redirect_uri/?code=CODE&amp;state=STATE"></a>（3）微信校验通过后，调用我们服务的redirect_uri/?code=CODE&amp;state=STATE</h5><h5 id="（4）通过code换取openid"><a href="#（4）通过code换取openid" class="headerlink" title="（4）通过code换取openid"></a>（4）通过code换取openid</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&amp;secret=SECRET&amp;code=CODE&amp;grant_type=authorization_code</span><br></pre></td></tr></table></figure>
<p>PS：通过上述接口，微信服务会返回openid</p>
<h3 id="步骤3-下单，获取prepay-id"><a href="#步骤3-下单，获取prepay-id" class="headerlink" title="步骤3. 下单，获取prepay_id"></a>步骤3. 下单，获取prepay_id</h3><p>调用微信公众号的统一下单接口</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">POST https://api.mch.weixin.qq.com/pay/unifiedorder</span><br><span class="line">&lt;xml&gt;</span><br><span class="line">&lt;appid&gt;wxbbe68ee8fadsf&lt;/appid&gt;</span><br><span class="line">&lt;attach&gt;<span class="built_in">test</span>&lt;/attach&gt;</span><br><span class="line">&lt;body&gt;腾讯充值中心-QQ会员充值&lt;/body&gt;</span><br><span class="line">&lt;mch_id&gt;1435628102&lt;/mch_id&gt;</span><br><span class="line">&lt;nonce_str&gt;1add1a30ac87aa2db72f57a2375d8fec&lt;/nonce_str&gt;</span><br><span class="line">&lt;notify_url&gt;http://www.weixin.qq.com/wxpay/pay.php&lt;/notify_url&gt;</span><br><span class="line">&lt;out_trade_no&gt;1415659999&lt;/out_trade_no&gt;</span><br><span class="line">&lt;spbill_create_ip&gt;110.90.119.97&lt;/spbill_create_ip&gt;</span><br><span class="line">&lt;scene_info&gt;&#123;<span class="string">"h5_info"</span>: &#123;<span class="string">"type"</span>:<span class="string">"Wap"</span>,<span class="string">"wap_url"</span>: <span class="string">"https://pay.qq.com"</span>,<span class="string">"wap_name"</span>: <span class="string">"腾讯充值"</span>&#125;&#125;&lt;/scene_info&gt;</span><br><span class="line">&lt;total_fee&gt;1&lt;/total_fee&gt;</span><br><span class="line">&lt;openid&gt;ouytHt3crrPI1s2LHAs&lt;/openid&gt;</span><br><span class="line">&lt;trade_type&gt;JSAPI&lt;/trade_type&gt;</span><br><span class="line">&lt;sign&gt;2B753269B6A69A46615D54AFD047AB2A&lt;/sign&gt;</span><br><span class="line">&lt;/xml&gt;</span><br></pre></td></tr></table></figure>
<p>PS: total_fee是按分计算，sign按微信定义计算，可以通过微信提供的 <a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1" target="_blank" rel="noopener">sign校验工具</a> 校验</p>
<p>如果成功会返回prepay_id</p>
<h3 id="步骤4-支付"><a href="#步骤4-支付" class="headerlink" title="步骤4. 支付"></a>步骤4. 支付</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">onBridgeReady</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">   WeixinJSBridge.invoke(</span><br><span class="line">       <span class="string">'getBrandWCPayRequest'</span>, &#123;</span><br><span class="line">           <span class="string">"appId"</span>:<span class="string">"wxbbe68ee8fadsf"</span>,     <span class="comment">//公众号名称，由商户传入</span></span><br><span class="line">           <span class="string">"timeStamp"</span>: <span class="string">'&#123;&#123;timeStamp&#125;&#125;'</span>,         <span class="comment">//时间戳，自1970年以来的秒数</span></span><br><span class="line">           <span class="string">"nonceStr"</span>:<span class="string">'&#123;&#123;nonceStr&#125;&#125;'</span>, <span class="comment">//随机串</span></span><br><span class="line">           <span class="string">"package"</span>: <span class="string">'&#123;&#123;prepayID&#125;&#125;'</span>,</span><br><span class="line">           <span class="string">"signType"</span>:<span class="string">"MD5"</span>,         <span class="comment">//微信签名方式：</span></span><br><span class="line">           <span class="string">"paySign"</span>:<span class="string">'&#123;&#123;paySign&#125;&#125;'</span> <span class="comment">//微信签名</span></span><br><span class="line">       &#125;,</span><br><span class="line">       <span class="function"><span class="keyword">function</span>(<span class="params">res</span>)</span>&#123;</span><br><span class="line">           alert(res.err_msg);</span><br><span class="line">           <span class="keyword">if</span>(res.err_msg == <span class="string">"get_brand_wcpay_request:ok"</span> ) &#123;&#125;     <span class="comment">// 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。</span></span><br><span class="line">       &#125;</span><br><span class="line">   );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>PS: prepayID填入上一步的prepay_id，paySign也是利用微信sign算法</p>
<p>上述过程完成后，即可弹出支付界面，用户输入支付密码确认支付后，即可完成，我们服务端根据回调接口notify_url进行后续的业务</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>通过以上过程，我们可以自定义链接（即二维码），微信扫描我们二维码，跳转至我们自己的H5界面，我们在H5展现商品信息，并包含”去支付”按钮，用户点击支付后，我们重定向至获取code接口；之后在我们自己服务端完成下单、并返回带支付信息的H5，用户点击确认支付按钮，JS调用微信公众号的WeixinJSBridge.invoke接口，弹出微信自身的支付界面，用户输入密码并支付，我们根据支付回调，调用后续业务。</p>
<p>以上过程，用户不需要关注公众号，非常便捷，适用于停车场等场景。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/08/17/2018-08-17-分析并解决Asterisk CPU占用过高问题/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/08/17/2018-08-17-分析并解决Asterisk CPU占用过高问题/" itemprop="url">分析并解决Asterisk CPU占用过高问题</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-17T00:00:00+08:00">
                2018-08-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/音视频/" itemprop="url" rel="index">
                    <span itemprop="name">音视频</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>现象：近来，发现Asterisk SIP服务经常占用CPU 100%以上，而且都在服务启动1个小时后出现，但重启服务后就恢复正常</p>
<p>初步分析：通过tshark对网络发包测试，并未发现有明显变化，因此不是请求量变大引发的CPU紧张（从重启服务恢复正常也可以证实），因此可以确定是服务内部的问题，得益于 <a href="https://moythreads.com/wordpress/2009/05/06/why-does-asterisk-consume-100-cpu/" target="_blank" rel="noopener">《Why does Asterisk consume 100% CPU?》</a> 这篇文章，我对asterisk服务进行了深度剖析，过程如下</p>
<h3 id="步骤1-获取哪个线程（Light-Weight-Process）占用CPU最高"><a href="#步骤1-获取哪个线程（Light-Weight-Process）占用CPU最高" class="headerlink" title="步骤1. 获取哪个线程（Light Weight Process）占用CPU最高"></a>步骤1. 获取哪个线程（Light Weight Process）占用CPU最高</h3><p>ps -LlFm -p <code>pidof asterisk</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">F S UID        PID  PPID   LWP  C NLWP PRI  NI ADDR SZ WCHAN    RSS PSR STIME TTY          TIME CMD</span><br><span class="line">4 - root     13090 13088     - 25   47   -   - - 522547 -     573636  - 08:43 pts/18   00:07:28 /usr/sbin/asterisk -f -vvvg -c</span><br><span class="line">4 S root         -     - 13090  0    -  80   0 -     - poll_s     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13091  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13092  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:17 -</span><br><span class="line">1 R root         -     - 13093  3    -  80   0 -     - -          -   0 08:43 -        00:01:01 -</span><br><span class="line">1 S root         -     - 13094  1    -  80   0 -     - futex_     -   1 08:43 -        00:00:19 -</span><br><span class="line">1 S root         -     - 13096  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13098  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:02 -</span><br><span class="line">1 S root         -     - 13100  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13101  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13102  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13103  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13104  2    -  80   0 -     - futex_     -   1 08:43 -        00:00:48 -</span><br><span class="line">1 S root         -     - 13105  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13106  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13107  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13108  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13109  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13110  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13111  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13112  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13113  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13114  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13115  0    -  80   0 -     - inet_c     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13116  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13117  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13118  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13119  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 R root         -     - 13120 16    -  80   0 -     - -          -   0 08:43 -        00:04:52 -</span><br><span class="line">1 S root         -     - 13121  0    -  80   0 -     - futex_     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13122  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13123  0    -  80   0 -     - poll_s     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13124  0    -  80   0 -     - hrtime     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13125  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13126  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13127  0    -  80   0 -     - futex_     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13128  0    -  80   0 -     - poll_s     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13129  0    -  80   0 -     - hrtime     -   1 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13130  0    -  80   0 -     - hrtime     -   0 08:43 -        00:00:00 -</span><br><span class="line">1 S root         -     - 13131  0    -  80   0 -     - poll_s     -   0 08:43 -        00:00:00 -</span><br></pre></td></tr></table></figure>
<p>其中C列表示线程占用CPU的百分比（这个值是自服务启动到目前的平均值，而top命令是实时，因此与top会不一致），从以上我们可以确定是13120线程占用最高，接下来我们要分析13120到底执行了什么</p>
<h3 id="步骤2-获取具体线程执行堆栈信息"><a href="#步骤2-获取具体线程执行堆栈信息" class="headerlink" title="步骤2. 获取具体线程执行堆栈信息"></a>步骤2. 获取具体线程执行堆栈信息</h3><p>pstack <code>pidof asterisk</code><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">Thread 21 (Thread 0x7fecd94ea700 (LWP 13120)):</span><br><span class="line">#0  0x00000000005aee3f in tps_cmp_cb ()</span><br><span class="line">#1  0x00000000004580a5 in internal_ao2_traverse ()</span><br><span class="line">#2  0x0000000000458b51 in __ao2_find ()</span><br><span class="line">#3  0x00000000005aef0d in ast_taskprocessor_create_with_listener ()</span><br><span class="line">#4  0x00000000005b4f1e in ast_threadpool_serializer ()</span><br><span class="line">#5  0x000000000059acc1 in internal_stasis_subscribe.clone.1 ()</span><br><span class="line">#6  0x00000000005a6168 in stasis_message_router_create_internal ()</span><br><span class="line">#7  0x00000000004ef227 in endpoint_internal_create ()</span><br><span class="line">#8  0x00007fecf0ec76c6 in build_peer () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#9  0x00007fecf0ed5a48 in realtime_peer () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#10 0x00007fecf0ed6646 in sip_find_peer_full () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#11 0x00007fecf0ed66b7 in sip_find_peer () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#12 0x00007fecf0f00aa7 in register_verify () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#13 0x00007fecf0f02cb5 in handle_request_register () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#14 0x00007fecf0f0d166 in handle_incoming () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#15 0x00007fecf0f0ed84 in handle_request_do () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#16 0x00007fecf0f10786 in sipsock_read () from /usr/lib/asterisk/modules/chan_sip.so</span><br><span class="line">#17 0x0000000000514b52 in ast_io_wait ()</span><br><span class="line">#18 0x00007fecf0ed4c6b in do_monitor () from /usr/lib/asterisk/modules/chan_sip.so</span><br></pre></td></tr></table></figure></p>
<p>从以上我们可以发现与创建taskprocessors有关，于是我们看下了taskprocessors数量</p>
<p>asterisk -rx ‘core show taskprocessors’</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">5d07f1f8-56be-4456-ac64-08dd619b09a9                   2            0            1</span><br><span class="line">88498730-b675-433d-b71c-eaa51fde68b6                   2            0            1</span><br><span class="line">eaabf337-81d5-4411-a0dc-a05f20071c7c                   2            0            1</span><br><span class="line">9dc17b55-f7cb-4ff4-bbcd-44033893eb4f                   2            0            1</span><br><span class="line">afa4cfff-4cb9-451b-a5d1-7ace2e4a9040                   2            0            0</span><br><span class="line">67729e31-8e78-423b-b5d5-dd746190f374                   2            0            0</span><br><span class="line">b9b5e393-f016-4d09-b8fe-03d7633ef174                   2            0            0</span><br><span class="line">    +---------------------+-----------------+------------+-------------+</span><br><span class="line">    55585 taskprocessors</span><br></pre></td></tr></table></figure>
<p>通过以上推测，应该是大量的taskprocessors占用的CPU资源，而且taskprocessors会不停增加</p>
<h3 id="步骤3：结合代码，确认原因"><a href="#步骤3：结合代码，确认原因" class="headerlink" title="步骤3：结合代码，确认原因"></a>步骤3：结合代码，确认原因</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">struct ast_taskprocessor *<span class="title">ast_taskprocessor_create_with_listener</span><span class="params">(<span class="keyword">const</span> <span class="keyword">char</span> *name, struct ast_taskprocessor_listener *listener)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">ast_taskprocessor</span> *<span class="title">p</span> = <span class="title">ao2_find</span>(<span class="title">tps_singletons</span>, <span class="title">name</span>, <span class="title">OBJ_KEY</span>);</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (p) &#123;</span><br><span class="line">        ast_taskprocessor_unreference(p);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> __allocate_taskprocessor(name, listener);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>ao2_find是根据name从任务队列中查找，一旦这个队列非常大，那CPU需要不停比较，从而会占用大量CPU资源，至此，问题基本确定了；接下来就要进入苦活阶段</p>
<h3 id="步骤4：解决代码BUG"><a href="#步骤4：解决代码BUG" class="headerlink" title="步骤4：解决代码BUG"></a>步骤4：解决代码BUG</h3><p>通过分析代码发现如下关系： peer-&gt;endpoint-&gt;route-&gt;subscription-&gt;mailbox</p>
<p>mailbox就是”struct ast_taskprocessor”即任务对象，因此可以推断应该是这个依赖链中有对象内存泄露了导致mailbox没有从任务队列中移除，通过一级一级分析，发现是peer内存泄露了，看如下代码</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (!strcasecmp(curi, <span class="string">"*"</span>) || !expire) &#123; <span class="comment">/* Unregister this peer */</span></span><br><span class="line">    <span class="comment">/* This means remove all registrations and return OK */</span></span><br><span class="line">    struct sip_peer* peer_db = sip_find_peer_by_db(peer-&gt;peername, <span class="literal">NULL</span>, TRUE, FINDPEERS, FALSE, <span class="number">0</span>);</span><br><span class="line">    <span class="comment">//ast_log(LOG_WARNING, "unregister found name%s\n",peer-&gt;peername);</span></span><br><span class="line">    <span class="keyword">if</span>(peer_db!=<span class="literal">NULL</span> &amp;&amp; peer-&gt;uuid!=<span class="literal">NULL</span> &amp;&amp; peer_db-&gt;uuid!=<span class="literal">NULL</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//ast_log(LOG_WARNING, "db_uuid%s,peer_uuid%s\n", peer_db-&gt;uuid,peer-&gt;uuid);</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="built_in">strcmp</span>(peer-&gt;uuid,peer_db-&gt;uuid) == <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            AST_SCHED_DEL_UNREF(sched, peer-&gt;expire,</span><br><span class="line">                sip_unref_peer(peer, <span class="string">"remove register expire ref"</span>));</span><br><span class="line">            ast_verb(<span class="number">3</span>, <span class="string">"Unregistered SIP '%s'\n"</span>, peer-&gt;name);</span><br><span class="line">            expire_register(sip_ref_peer(peer,<span class="string">"add ref for explicit expire_register"</span>));</span><br><span class="line">            <span class="keyword">return</span> PARSE_REGISTER_UPDATE;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">            ast_log(LOG_WARNING, <span class="string">"Invalid unregister db_uuid%s,but peer_uuid%s\n"</span>, peer_db-&gt;uuid,peer-&gt;uuid);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>peer_db是sip_find_peer_by_db得到的，而sip_find_peer_by_db是有’加引用’的，因此在没用peer_db时需要执行’去引用’，因此此处需要加sip_unref_peer(peer_db, “remove”)</p>
<p>当然代码中还有其他地方也是类似，这里不一一列出，改完所有泄露，重新编译运行发现taskprocessors不会不断增加，维持在30个以内，通过几个小时的运行，CPU没有增高，此问题得到的最终解决。</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>【授之以鱼，不如授之以渔】以上只是一种CPU过高的原因，各位很可能遇到的问题不一样，但都可以借鉴上面步骤对问题进行排查。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/08/14/2018-08-14-lua调用c/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/08/14/2018-08-14-lua调用c/" itemprop="url">lua调用c</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-14T00:00:00+08:00">
                2018-08-14
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/其他/" itemprop="url" rel="index">
                    <span itemprop="name">其他</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>本文介绍lua如何调用c方法</p>
<h4 id="编写c"><a href="#编写c" class="headerlink" title="编写c"></a>编写c</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;lua.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;lauxlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;lualib.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">int</span> <span class="title">echo</span><span class="params">(lua_State* L)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">char</span>* str = lua_tostring(L, <span class="number">1</span>);</span><br><span class="line">    lua_pushstring(L, str);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">luaL_Reg</span> <span class="title">mapper</span>[] = &#123;</span></span><br><span class="line">    &#123;<span class="string">"echo"</span>, echo&#125;,</span><br><span class="line">    &#123;<span class="literal">NULL</span>, <span class="literal">NULL</span>&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">extern</span> <span class="keyword">int</span> <span class="title">luaopen_hello</span><span class="params">(lua_State* L)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    luaL_newlib(L, mapper);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>luaopen_后面的命名要与so名称一致</li>
<li>5.2版本可能没有luaL_newlib，需要使用luaL_openlib替代，如下</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">luaL_openlib(L, <span class="string">"hello"</span>, mapper, <span class="number">0</span>);</span><br></pre></td></tr></table></figure>
<h4 id="编译so"><a href="#编译so" class="headerlink" title="编译so"></a>编译so</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc hello.c -shared -o hello.so -llua</span><br></pre></td></tr></table></figure>
<h4 id="运行"><a href="#运行" class="headerlink" title="运行"></a>运行</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">smou:luac $ lua -i</span><br><span class="line">Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio</span><br><span class="line">&gt; hello=require <span class="string">'hello'</span></span><br><span class="line">&gt; hello.echo(<span class="string">'123'</span>)</span><br><span class="line">123</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/08/13/2018-08-13-Lua编写wireshark插件/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/08/13/2018-08-13-Lua编写wireshark插件/" itemprop="url">Lua编写wireshark插件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-13T00:00:00+08:00">
                2018-08-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/技巧/" itemprop="url" rel="index">
                    <span itemprop="name">技巧</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>在我们自己的系统中，会自定义协议，甚至加密；为了更容易进行抓包分析，我们需要编写wireshark插件.</p>
<h3 id="前提"><a href="#前提" class="headerlink" title="前提"></a>前提</h3><p>wireshark需要支持Lua，可以打开Wireshark，进入关于界面，若带有”with Lua x.x.x”，则表示支持，否则请升级; 命令行方式可以使用<code>tshark -v</code>查看，我的版本信息如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">TShark (Wireshark) 2.2.3 (v2.2.3-0-g57531cd)</span><br><span class="line"></span><br><span class="line">Copyright 1998-2016 Gerald Combs &lt;gerald@wireshark.org&gt; and contributors.</span><br><span class="line">License GPLv2+: GNU GPL version 2 or later &lt;http://www.gnu.org/licenses/old-licenses/gpl-2.0.html&gt;</span><br><span class="line">This is free software; see the <span class="built_in">source</span> <span class="keyword">for</span> copying conditions. There is NO</span><br><span class="line">warranty; not even <span class="keyword">for</span> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.</span><br><span class="line"></span><br><span class="line">Compiled (64-bit) with libpcap, without POSIX capabilities, with GLib 2.36.0,</span><br><span class="line">with zlib 1.2.5, with SMI 0.4.8, with c-ares 1.12.0, with lua 5.2.4, with GnuTLS</span><br><span class="line">2.12.19, with Gcrypt 1.5.0, with MIT Kerberos, with GeoIP.</span><br><span class="line"></span><br><span class="line">Running on Mac OS X 10.12.6, build 16G29 (Darwin 16.7.0), with locale</span><br><span class="line">C/UTF-8/C/C/C/C, with libpcap version 1.8.1 -- Apple version 67.60.1, with</span><br><span class="line">GnuTLS 2.12.19, with Gcrypt 1.5.0, with zlib 1.2.8.</span><br><span class="line">Intel(R) Core(TM) i5-4258U CPU @ 2.40GHz (with SSE4.2)</span><br><span class="line"></span><br><span class="line">Built using llvm-gcc 4.2.1 (Based on Apple Inc. build 5658) (LLVM build</span><br><span class="line">2336.9.00).</span><br></pre></td></tr></table></figure>
<h3 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h3><ul>
<li>界面</li>
</ul>
<p>打开wireshark安装目录的init.lua文件，在最后添加”dofile(…)”</p>
<p>vim /Applications/Wireshark.app/Contents/Resources/share/wireshark/init.lua<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">DATA_DIR = Dir.global_config_path()</span><br><span class="line">USER_DIR = Dir.personal_config_path()</span><br><span class="line"></span><br><span class="line">dofile(DATA_DIR..<span class="string">"console.lua"</span>)</span><br><span class="line">dofile(USER_DIR..<span class="string">"xxxxx.lua"</span>)</span><br></pre></td></tr></table></figure></p>
<p>PS: DATA_DIR表示全局配置路径，USER_DIR表示用户配置路径，可以通过【”关于Wireshark” -&gt; “文件夹”】查看全局配置和用户路径</p>
<ul>
<li>命令行</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tshark -X lua_script:xxx.lua</span><br></pre></td></tr></table></figure>
<p>PS: xxx.lua可以带路径，但不能用~</p>
<p>PS: init.lua同样适用于命令行，若init.lua已经配置，则不能用-X再次指定，否则会报存在2个一样的协议</p>
<h3 id="编写lua"><a href="#编写lua" class="headerlink" title="编写lua"></a>编写lua</h3><p>如下是模板代码，可以在此基础上进行扩展</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 获取对应解析表，DissectorTable.get(tablename)</span></span><br><span class="line"><span class="comment">-- 可以通过"Decode As..."查看所有表名，本例是添加udp 4444端口的解析</span></span><br><span class="line"><span class="keyword">local</span> udp_table = DissectorTable.get(<span class="string">"udp.port"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建协议，Proto.new(name, desc), name不能有空格</span></span><br><span class="line"><span class="keyword">local</span> my_proto = Proto(<span class="string">"myproto"</span>, <span class="string">"my proto description"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 定义字段内容 ProtoField.new(name, abbr, base), base表示以什么方式展现，有base.DEC, base.HEX, base.OCT, base.DEC_HEX, base.DEC_HEX or base.HEX_DEC</span></span><br><span class="line"><span class="keyword">local</span> magicField = ProtoField.uint16(<span class="string">"Magic"</span>, <span class="string">"Magic"</span>, base.HEX)</span><br><span class="line">my_proto.fields = &#123;magicField&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">--协议解析，buffer是数据部分，pinfo是wireshark列表信息，有Protocol, Info等</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">my_proto.dissector</span><span class="params">(buffer, pinfo, tree)</span></span></span><br><span class="line"></span><br><span class="line">    <span class="comment">-- 在列表的Protocol栏目中展现</span></span><br><span class="line">    pinfo.cols.protocol:set(<span class="string">"MYPROTO"</span>)</span><br><span class="line">    <span class="keyword">local</span> <span class="built_in">len</span> = buffer:<span class="built_in">len</span>()</span><br><span class="line"></span><br><span class="line">    <span class="comment">-- 在列表的Info栏目中展现</span></span><br><span class="line">    pinfo.cols.info = <span class="string">'HELLO MY PROTO'</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">-- 添加自定义协议根节点</span></span><br><span class="line">    <span class="keyword">local</span> myProtoTree = tree:add(my_proto, buffer(<span class="number">0</span>, <span class="built_in">len</span>), <span class="string">"MYPROTO Protocol"</span>)</span><br><span class="line">    <span class="keyword">local</span> offset = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">-- 添加字段</span></span><br><span class="line">    myProtoTree:add(magicField, buffer(offset, <span class="number">2</span>))</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="comment">--增加协议</span></span><br><span class="line">udp_table:add(<span class="number">4444</span>, my_proto)</span><br></pre></td></tr></table></figure>
<h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">smou:~ $ tshark -i en0 -X lua_script:test.lua port 4444</span><br><span class="line">  1   0.000000 192.168.113.75 → 120.24.89.194 MYPROTO 47 HELLO MY PROTO</span><br></pre></td></tr></table></figure>
<p>PS: 可以添加-V展现树节点</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/08/13/2018-08-13-Lua编写带有so的Wireshark插件/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/08/13/2018-08-13-Lua编写带有so的Wireshark插件/" itemprop="url">Lua编写带有so的Wireshark插件</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-13T00:00:00+08:00">
                2018-08-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/技巧/" itemprop="url" rel="index">
                    <span itemprop="name">技巧</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>wireshark支持纯lua和c插件，那么如何支持lua和c混编的问题，即lua调用c接口，对于lua语言，这个不是问题，但加上环境，就会出现如下问题</p>
<h2 id="问题1-如何编译正确的so"><a href="#问题1-如何编译正确的so" class="headerlink" title="问题1. 如何编译正确的so"></a>问题1. 如何编译正确的so</h2><p>直接通过以下语句，就可以生成so<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -fPIC -shared -o test.so -llua</span><br></pre></td></tr></table></figure></p>
<p>通过以上语句确实可以生成so，但链接的是系统lua库，一旦系统lua与wireshark自带lua版本不一致，就可以导致实际无法运行</p>
<p>这里讲个实际的例子，我系统lua是5.3.4是支持luaL_newlib，但wireshark却是5.2.3，没有luaL_newlib，因此会报找不到对应symbol，而需要改为luaL_openlib</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">extern</span> <span class="keyword">int</span> <span class="title">luaopen_test</span><span class="params">(lua_State* L)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    luaL_openlib(L, <span class="string">"test"</span>, mytest, <span class="number">0</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>因此正确做法，需要链接wireshark对应的动态库</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -fPIC -shared -o test.so -L/Applications/Wireshark.app/Contents/Frameworks -lwireshark.8.1.3</span><br></pre></td></tr></table></figure>
<h3 id="问题2-如何让wireshark加载so"><a href="#问题2-如何让wireshark加载so" class="headerlink" title="问题2. 如何让wireshark加载so"></a>问题2. 如何让wireshark加载so</h3><p>lua中添加</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">test=<span class="built_in">require</span> <span class="string">'test'</span></span><br></pre></td></tr></table></figure>
<p>打开Wireshark，会给出如下提示</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Lua: Error during loading:</span><br><span class="line"> /Users/smou/.config/wireshark/test.lua:6: module &apos;test&apos; not found:</span><br><span class="line">    no field package.preload[&apos;test&apos;]</span><br><span class="line">    no file &apos;/usr/local/share/lua/5.2/test.lua&apos;</span><br><span class="line">    no file &apos;/usr/local/share/lua/5.2/test/init.lua&apos;</span><br><span class="line">    no file &apos;/usr/local/lib/lua/5.2/test.lua&apos;</span><br><span class="line">    no file &apos;/usr/local/lib/lua/5.2/test/init.lua&apos;</span><br><span class="line">    no file &apos;./test.lua&apos;</span><br><span class="line">    no file &apos;/usr/local/lib/lua/5.2/test.so&apos;</span><br><span class="line">    no file &apos;/usr/local/lib/lua/5.2/loadall.so&apos;</span><br><span class="line">    no file &apos;./test.so&apos;</span><br></pre></td></tr></table></figure>
<p>PS: 我们只要将so拷贝到/usr/local/lib/lua/5.2/中即可</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/08/13/2018-08-13-sed提取一行中匹配的字符串/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/08/13/2018-08-13-sed提取一行中匹配的字符串/" itemprop="url">sed提取一行中匹配的字符串</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-13T00:00:00+08:00">
                2018-08-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/技巧/" itemprop="url" rel="index">
                    <span itemprop="name">技巧</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">提取123</span><br><span class="line"><span class="built_in">echo</span> <span class="string">'abcdef123liek'</span>|sed <span class="string">'s/.*ef\(.*\)liek/\1/g'</span></span><br></pre></td></tr></table></figure>
<p>PS: ()中的内容可用1、2、3等数字表示且需要加转义符\</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> <span class="string">'abcdef123liekliek'</span>|sed <span class="string">'s/.*ef\(.*\)liek.*/\1/g'</span></span><br></pre></td></tr></table></figure>
<p>PS: liek匹配最后一个，而不是第一个</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://smallmuou.gitee.io/blog/blog/2018/07/27/2018-07-27-重定义Linux的rm命令/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="smallmuou">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="smallmuou's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/2018/07/27/2018-07-27-重定义Linux的rm命令/" itemprop="url">重定义Linux的rm命令</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-07-27T00:00:00+08:00">
                2018-07-27
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/运维/" itemprop="url" rel="index">
                    <span itemprop="name">运维</span>
                  </a>
                </span>

                
                
              
            </span>
          

          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><code>rm -rf</code>可以说是非常致命，删除之后比较难恢复，因此我们尽量减少直接使用，可以参考mac、window都有一个回收站，因此我们可以重定义rm命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">echo</span> -e <span class="string">'alias rm=trash\ntrash() &#123;\n mkdir -p ~/.trash;/bin/mv $@ ~/.trash/\n&#125;'</span> &gt;&gt; ~/.bash_profile</span><br></pre></td></tr></table></figure>
<p>这样即使我们误删，也可以从~/.trash中找到，但要记得定时清理回收站</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/blog/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/blog/">1</a><span class="page-number current">2</span><a class="page-number" href="/blog/page/3/">3</a><span class="space">&hellip;</span><a class="page-number" href="/blog/page/20/">20</a><a class="extend next" rel="next" href="/blog/page/3/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">smallmuou</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">197</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/smallmuou" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-globe"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">smallmuou</span>

  
</div>

    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <span id="busuanzi_container_site_pv">总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
